/**
Shows the frequency spectrum of line-in
 */

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.signals.*;

Minim minim;
AudioInput in;
AudioOutput out;
AudioRecorder recorder;
FFT fft;
FFT referencefft;
SineWave sine;

void setup()
{
  size(800, 400, P2D);
  textMode(SCREEN); 
  
  minim = new Minim(this);

  // get a stereo line-in: sample buffer length of 2048
  // default sample rate is 44100, default bit depth is 16
  in = minim.getLineIn(Minim.STEREO, 2048);
  out = minim.getLineOut(Minim.STEREO,2048);
  // create a recorder that  will record from the input to the filename specified, using buffered recording
  // buffered recording means that all captured audio will be written into a sample buffer
  // then when save() is called, the contents of the buffer will actually be written to a file
  // the file will be located in the sketch's root folder.
  recorder = minim.createRecorder(in, "myrecording.wav", true);
  fft = new FFT(in.bufferSize(), in.sampleRate()); 
  referencefft = new FFT(in.bufferSize(), in.sampleRate()); 
  fft.logAverages(22, 32);
  referencefft.logAverages(22, 32);
  sine = new SineWave(220, 2, out.sampleRate());
//  out.addSignal(sine);
  textFont(createFont("SanSerif", 12));
  rectMode(CORNERS);
}

void draw()
{
  background(0); 
  stroke(255);
  // draw the waveforms

  fft.forward(in.mix);
  referencefft.forward(out.mix);
  int w = int(width/fft.avgSize());
  for(int i = 16; i < fft.avgSize(); i++)
  {
    // draw a rectangle for each average, multiply the value by 2 so we can see it better
    stroke(255,0,0);
    fill(255,0,0);
    float sig = 14*fft.getAvg(i);
    if(sig<30)sig=0;
//    else sig = 200;
    rect(i*w, height/2-30, i*w + w, height/2-30 - sig);
    if(i%32==0){
      stroke(255);
      fill(255);
      rect(i*w, height/2-30, i*w +1, 0);
      text((int)(i/1024.0*44100),i*w,height/2);
    }
    stroke(180);
    if(i%4==0){
      rect(i*w, height/2-30, i*w, 0);
    }

  }
 

  fft.forward(in.mix);
  referencefft.forward(out.mix);
  for(int i = 0; i < fft.specSize(); i++){
    // draw the line for frequency band i, scaling it by 4 so we can see it a bit better
    float sig = fft.getBand(i)*14;
//    if(sig>height*0.8)sig=height*0.8;
//    if(sig<height*0.1)sig=0;
    line(i*1.5, height, i*1.5, height - sig);
//reference
    float referencesig = fft.getBand(i)*14;
    line(i*1.5, height, i*1.5, height - referencesig);

  }

  
  //fill(255);
  // keep us informed about the window being used
//  text("The window being used is: " + windowName, 5, 20);
}
  
  if ( recorder.isRecording() )
  {
    text("Currently recording...", 5, 15);
  }
  else
  {
    text("Not recording.", 5, 15);
  }
}

void keyReleased()
{
  if ( key == 'r' ) 
  {
    // to indicate that you want to start or stop capturing audio data, you must call
    // beginRecord() and endRecord() on the AudioRecorder object. You can start and stop
    // as many times as you like, the audio data will be appended to the end of the buffer 
    // (in the case of buffered recording) or to the end of the file (in the case of streamed recording). 
    if ( recorder.isRecording() ) 
    {
      recorder.endRecord();
    }
    else 
    {
      recorder.beginRecord();
    }
  }
  if ( key == 's' )
  {
    // we've filled the file out buffer, 
    // now write it to the file we specified in createRecorder
    // in the case of buffered recording, if the buffer is large, 
    // this will appear to freeze the sketch for sometime
    // in the case of streamed recording, 
    // it will not freeze as the data is already in the file and all that is being done
    // is closing the file.
    // the method returns the recorded audio as an AudioRecording, 
    // see the example  AudioRecorder >> RecordAndPlayback for more about that
    recorder.save();
    println("Done saving.");
  }
}

void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();
  
  super.stop();
}
